package com.example.testlibraryproject.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
 * Class to act as geteway to get content of internet pages in form of String
 * @author Dayanand
 *
 */
public class LibWebServiceUtils {
	static String tag  = "##WebServiceUtility##";
	static int TIMEOUT_MILLISEC = 5000; 
	/**
	 * Ws to execute given url and return data as string,
	 * 
	 * @param url
	 * @return data as string or null if error occurred
	 */
	public static String executeGet(String url){
		Log.v(tag, "WS URL::"+url);
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
		HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);



		DefaultHttpClient client = new DefaultHttpClient(httpParams);
		HttpGet request = new HttpGet(url);

		try {

			HttpResponse response = client.execute(request);
			final int statusCode = response.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) {
				Log.v(tag,"Error " + statusCode
						+ " for URL " + url);
				return null;
			}

			HttpEntity responseEntity = response.getEntity();
			String result = EntityUtils.toString(responseEntity);
			//L.v("WS RESULt:: "+result);
			return result;

		} catch (IOException e) {
			request.abort();
			e.printStackTrace();
			Log.v(tag,"Error for URL " + url+"error:"+e.getMessage());
		}

		return null;

	}

	/**
	 * Function to act as gateway to execute http post request and to return the result in string format
	 * @param url
	 * @param paramsMap
	 * @return string result content, null if any error occurred
	 */
	public static String executePost(String url, HashMap<String, String> paramsMap){
		Log.v(tag, "WS URL::"+url);
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
		HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);



		DefaultHttpClient client = new DefaultHttpClient(httpParams);
		HttpPost httpost = new HttpPost(url);

		//adding post parameters to request
		if(paramsMap!=null && paramsMap.size()>0){
			int postParamsSize = paramsMap.size();
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(postParamsSize);

			Iterator<Map.Entry<String, String>> params = paramsMap.entrySet().iterator();

			while(params.hasNext()){
				Map.Entry<String, String> param = params.next();
				nameValuePairs.add(new BasicNameValuePair(param.getKey(), param.getValue()));

			}
			//adding name value pairs to reqeust	
			try {
				httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.v(tag, "error:"+e.getMessage());
			}
		}

		try {

			HttpResponse response = client.execute(httpost);
			final int statusCode = response.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) {
				Log.v(tag,"Error " + statusCode
						+ " for URL " + url);
				return null;
			}

			HttpEntity responseEntity = response.getEntity();
			String result = EntityUtils.toString(responseEntity);
			//L.v("WS RESULt:: "+result);
			return result;

		} catch (IOException e) {
			httpost.abort();
			e.printStackTrace();
			Log.v(tag,"Error for URL " + url+"error:"+e.getMessage());
		}

		return null;

	}
}
